package com.azusa.xbjy.controller;

import com.azusa.xbjy.entity.Article;
import com.azusa.xbjy.entity.Result;
import com.azusa.xbjy.entity.StatusCode;
import com.azusa.xbjy.entity.User;
import com.azusa.xbjy.service.ArticleService;
import com.azusa.xbjy.utils.IdWorker;
import com.azusa.xbjy.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private IdWorker idWorker;


    @RequestMapping("toArticle")
    public ModelAndView toArticle(){
        return new ModelAndView("article/article");
    }

    @RequestMapping("toArticleAdd")
    public ModelAndView toArticleAdd(){
        return new ModelAndView("article/article_add");
    }

    @RequestMapping("toArticleCollect")
    public ModelAndView toArticleCollect(){
        return new ModelAndView("article/article_collect");
    }

    @RequestMapping("toArticleDetail")
    public ModelAndView toArticleDetail(){
        return new ModelAndView("article/article_detail");
    }

    @RequestMapping("findArticlePage/{pageNum}/{pageSize}")
    public Result findArticlePage(@PathVariable("pageNum")int pageNum, @PathVariable("pageSize")int pageSize, @RequestBody Map<String,Object> map){
        return new Result(StatusCode.SUCCESS,articleService.findAllPage(pageNum,pageSize,(String)map.get("title")));
    }

    /**
     * 发布文章
     * @param article
     * @param session
     * @return
     */
    @PostMapping("insertArticle")
    public Result insertArticle(@RequestBody Article article, HttpSession session){
        User user = LoginUserUtil.getLoginUser();
        article.setId((int) idWorker.nextId());
        article.setPublishRealName(user.getRealName());
        article.setUserId(user.getId());
        article.setBrowseCount(0);
        article.setPublishDate(new Date());
        articleService.save(article);
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 查询文章详情
     * @param aid
     * @param uid
     * @return
     */
    @RequestMapping("findArticleDetail/{aid}/{uid}")
    public Result findArticleDetail(@PathVariable("aid")int aid,@PathVariable("uid")int uid){
        return new Result(StatusCode.SUCCESS,articleService.findById(aid, uid));
    }

    /**
     * 修改文章浏览数+1
     * @param aid
     * @return
     */
    @RequestMapping("updateBrowseCount/{aid}")
    public Result updateBrowseCount(@PathVariable("aid")int aid){
        articleService.updateBrowseCount(aid);
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 取消收藏/收藏文章
     * @param aid
     * @param uid
     * @param collect
     * @return
     */
    @PutMapping("updateCollect/{aid}/{uid}/{collect}")
    public Result updateCollect(@PathVariable("aid")int aid,@PathVariable("uid")int uid,@PathVariable("collect")boolean collect){
        articleService.updateCollect(aid,uid,collect);
        return new Result(StatusCode.SUCCESS);
    }

    @PutMapping("findCollectPage/{pageNum}/{pageSize}")
    public Result findCollectPage(@PathVariable("pageNum")int pageNum, @PathVariable("pageSize")int pageSize,@RequestBody Map<String,Object> params){
        return new Result(StatusCode.SUCCESS,articleService.findCollectPage(pageNum,pageSize,Integer.valueOf((String) params.get("userId")),(String) params.get("title")));
    }
}
